using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace BowMaster
{

    public enum TextPos
    {
        Center,
        Right,
        Left
    }
    public class MyButton
    {
        public String text;
        public SpriteFont font;
        public Vector2 position;
        public Color textColor;
        public Color textureColor;
        Texture2D texture;
        public Rectangle rectangle;
        public bool enabled;
        public float textScale;
        
        public TextPos textPos;
        public float textRotation;
        private float prevTextRotation;
        private Vector2 rotationCenter;
        public MyButton(Rectangle rectangle, String text, Color textColor, SpriteFont font, TextPos textPos, Texture2D texture, Color textureColor)
        {

            this.textColor = textColor;
            this.font = font;
            this.text = text;
            this.rectangle = rectangle;
            this.texture = texture;
            this.textureColor = textureColor;
            this.enabled = true;
            textScale = 1;
            this.textPos = textPos;
            UpdateTextPos();
            prevTextRotation = textRotation = 0;
            rotationCenter = Vector2.Zero;
        }
        public MyButton(Rectangle rectangle, String text, Color textColor, SpriteFont font)
            : this(rectangle, text, textColor, font, TextPos.Center, null, Color.Transparent)
        { }
        public MyButton(Rectangle rectangle, String text, Color textColor, SpriteFont font, TextPos textPos)
            : this(rectangle, text, textColor, font, textPos, null, Color.Transparent)
        {

        }
        public MyButton(Rectangle rectangle, Texture2D texture, Color textureColor)
            : this(rectangle, "", Color.Transparent, null, TextPos.Center, texture, textureColor)
        { }
        public void Draw(SpriteBatch batch)
        {
            if (!enabled)
                return;
            //position = OptionsMenu.centerString(rectangle, text, font, textScale);
            if (texture != null)
                batch.Draw(texture, rectangle, textureColor);
            if (font != null)
            {
                if (textRotation != prevTextRotation)
                {
                    prevTextRotation = textRotation;
                    rotationCenter = font.MeasureString(text) / 2;
                }
                if (textPos == TextPos.Center)
                    centerString();
                if(textRotation==0)
                    batch.DrawString(font, text, position, textColor);
                else
                    batch.DrawString(font, text, position, textColor, textRotation, font.MeasureString(text) / 2, textScale, SpriteEffects.None, 1);
            }
        }
        public bool tapped_on(Point p)
        {
            return enabled && rectangle.Contains(p);
        }


        public Vector2 centerString(Rectangle rectangle, string text, SpriteFont spriteFont)
        {
            Vector2 destinazione = new Vector2();
            if (text == null || text == "") return new Vector2(rectangle.Center.X, rectangle.Center.Y);
            Vector2 dimStringa = spriteFont.MeasureString(text);

            destinazione.X = rectangle.X + rectangle.Width / 2 - dimStringa.X / 2;
            destinazione.Y = rectangle.Y + rectangle.Height / 2 - dimStringa.Y / 2;

            return destinazione;
        }
        public Vector2 centerString(Rectangle rectangle, string text, SpriteFont spriteFont, float scale)
        {
            Vector2 destinazione = new Vector2();
            if (text == null || text == "") return new Vector2(rectangle.Center.X, rectangle.Center.Y);
            Vector2 dimStringa = spriteFont.MeasureString(text)*scale;

            destinazione.X = rectangle.X + rectangle.Width / 2 - dimStringa.X / 2;
            destinazione.Y = rectangle.Y + rectangle.Height / 2 - dimStringa.Y / 2;

            return destinazione;
        }
        public void centerString()
        {

            if (text == null || text == "")
            {
                return;
            }
            Vector2 dimStringa = font.MeasureString(text);

            position.X = rectangle.X + rectangle.Width / 2 - dimStringa.X / 2;
            position.Y = rectangle.Y + rectangle.Height / 2 - dimStringa.Y / 2;

        }
        public void UpdateTextPos()
        {
            if (textPos == TextPos.Center)
            {
                position = centerString(rectangle, text, font, textScale);
            }
            else if (textPos == TextPos.Left)
            {
                position = centerString(rectangle, text, font, textScale);
                position.X = rectangle.X + 5;
            }
            else
                if (textPos == TextPos.Right)
                {
                    position = centerString(rectangle, text, font, textScale);
                    position.X = rectangle.X + rectangle.Width - 5 - font.MeasureString(text).Length();
                }
        }
    }



    //public class MyButton
    //{
    //    public Rectangle rectangle;
    //    public String text;
    //    public bool textOnCenter;
    //    public Color color;
    //    public Texture2D texture;
    //    public SpriteFont font;

    //    public Vector2 textPosition;
    //    private Vector2 dimStringa;
    //    public Color textColor;
    //    public float textRotation;
    //    public float textScale;
    //    public bool enabled;
    //    private Rectangle tappableRectangle;
    //    private int tappableOffset = 10;
    //    public MyButton(Rectangle rectangle, String text, Color textColor, Vector2 textPosition, SpriteFont font, Color color, Texture2D texture)
    //    {
    //        this.rectangle = rectangle;
    //        this.text = text;
    //        this.textPosition = textPosition;
    //        this.color = color;
    //        textOnCenter = false;
    //        this.texture = texture;
    //        this.font = font;
    //        this.textColor = textColor;
    //        textRotation = 0;
    //        textScale = 1;
    //        enabled = true;
    //        tappableRectangle = new Rectangle(rectangle.X - tappableOffset,
    //            rectangle.Y - tappableOffset, rectangle.Width + tappableOffset * 2,
    //            rectangle.Height + tappableOffset * 2);
    //    }


    //    public MyButton(Rectangle rectangle, String text, Color textColor, SpriteFont font, Color color, Texture2D texture)
    //        : this(rectangle, text, textColor, Vector2.Zero, font, color, texture)
    //    {
    //        textOnCenter = true;
    //    }

    //    public void Draw(SpriteBatch spriteBatch)
    //    {
    //        spriteBatch.Draw(texture, rectangle, color);
    //        if (textOnCenter && font!=null)
    //            centerString();
    //        if (font != null)
    //            spriteBatch.DrawString(font, text, textPosition, textColor, textRotation, font.MeasureString(text) / 2, textScale, SpriteEffects.None, 1);
    //    }

    //    public bool tapped_on(Point tp)
    //    {
    //        tappableRectangle.X = rectangle.X - tappableOffset;
    //        tappableRectangle.Y = rectangle.Y - tappableOffset;
    //        tappableRectangle.Width = rectangle.Width + tappableOffset * 2;
    //        tappableRectangle.Height = rectangle.Height + tappableOffset * 2;
    //        return enabled && tappableRectangle.Contains(tp);
    //    }

    //    private void centerString()
    //    {
    //        //Vector2 destinazione = new Vector2();
    //        if (text == null || text == "")
    //        {
    //            textPosition.X = rectangle.Center.X;
    //            textPosition.Y = rectangle.Center.Y;
    //            return;
    //        }
    //        dimStringa = font.MeasureString(text)*textScale;

    //        textPosition.X = rectangle.X + rectangle.Width / 2 - dimStringa.X / 2;
    //        textPosition.Y = rectangle.Y + rectangle.Height / 2 - dimStringa.Y / 2;

    //        textPosition += font.MeasureString(text) * textScale / 2.0f;
    //    }
    //}
}
